package com.huike.web.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.huike.clues.domain.FalseClues;
import com.huike.clues.service.ICluesService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import com.huike.clues.domain.Clues;
import com.huike.common.utils.poi.ExcelUtil;
import com.huike.common.core.page.TableDataInfo;

/**
 * 线索Controller
 * 
 * @author ruoyi
 * @date 2024-07-07
 */
@RestController
@RequestMapping("/clues/clue")
public class CluesController extends BaseController
{
    @Autowired
    private ICluesService cluesService;

    /**
     * 查询线索列表
     */
    @PreAuthorize("@ss.hasPermi('system:clues:list')")
    @GetMapping("/list")
    public TableDataInfo list(Clues clues)
    {
        startPage();
        List<Clues> list = cluesService.selectCluesList(clues);
        return getDataTable(list);
    }

    /**
     * 导出线索池列表
     */
    @PreAuthorize("@ss.hasPermi('system:clues:pool:list')")
    @GetMapping("/pool")
    public TableDataInfo poolList(Clues clues){
        startPage();
        List<Clues> list = cluesService.selectCluesPoolList(clues);
        return getDataTable(list);
    }


    /**
     * 导出线索列表
     */
    @PreAuthorize("@ss.hasPermi('system:clues:export')")
    @Log(title = "线索", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Clues clues)
    {
        List<Clues> list = cluesService.selectCluesList(clues);
        ExcelUtil<Clues> util = new ExcelUtil<Clues>(Clues.class);
        util.exportExcel(list, "线索数据");
    }

    /**
     * 获取线索详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:clues:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(cluesService.selectCluesById(id));
    }

    /**
     * 新增线索
     */
    @PreAuthorize("@ss.hasPermi('system:clues:add')")
    @Log(title = "线索", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Clues clues)
    {
        return toAjax(cluesService.insertClues(clues));
    }

    /**
     * 修改线索
     */
    @PreAuthorize("@ss.hasPermi('system:clues:edit')")
    @Log(title = "线索", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Clues clues)
    {
        return toAjax(cluesService.updateClues(clues));
    }

    /**
     * 删除线索
     */
    @PreAuthorize("@ss.hasPermi('system:clues:remove')")
    @Log(title = "线索", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(cluesService.deleteCluesByIds(ids));
    }

    /**
     * 伪线索上报
     */
    @PreAuthorize("@ss.hasPermi('system:clues:false')")
    @PutMapping("/false/{id}")
    public AjaxResult falseCule(
            @PathVariable Long id,
            @RequestBody FalseClues falseClues
            )
    {
        falseClues.setCreateBy(SecurityUtils.getUsername());
        return cluesService.updateClueFalse(id,falseClues);
    }

    /**捞取线索 */
    @PreAuthorize("@ss.hasPermi('system:clues:gain')")
    @PutMapping("/gain")
    public AjaxResult gainCule(@RequestBody Map<String,List<Long>> map){
        List<Long> ids = map.get("ids");
        return toAjax(cluesService.updateOwner(ids));
    }

    /**转为商机 */
    @PreAuthorize("@ss.hasPermi('system:clues:changeBusiness')")
    @PutMapping("/changeBusiness/{id}")
    public AjaxResult changeBusiness(@PathVariable Long id){
        return toAjax(cluesService.changeBusiness(id.toString()));
    }
}
